package com.hb.znpt.model;

import com.hb.znpt.BuildConfig;
import com.hb.znpt.bean.CheckBean;
import com.hb.znpt.utils.AESHttpUtil;
import com.hb.znpt.utils.Constants;
import com.hb.znpt.utils.GCJ2WGS;
import com.hb.znpt.utils.LogUtils;
import com.hb.znpt.utils.OssManager;
import com.unitlib.base.base.BaseModel;
import com.unitlib.base.utils.SPUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import rxhttp.wrapper.param.RxHttp;

/**
 * @Description: 描述
 * @Author: houbing
 * @CreateDate: 2021/9/9 16:58
 */
public class UploadModel extends BaseModel {

    Calendar calendar = Calendar.getInstance();

    public Observable<String> upload(double lat, double lon, String purl, String zwid) {
        int cunYear = calendar.get(Calendar.YEAR);
        long t = new Date().getTime();
        HashMap<String, Double> pos = GCJ2WGS.delta(lat, lon);
        String url = OssManager.POINT + "/" + purl;
        String json = "{\"year\":\"" + cunYear +
                "\",\"sfz\":\"" + SPUtils.getInstance().getString(Constants.SP_USERCARD) +
                "\",\"token\":\"" + SPUtils.getInstance().getString(Constants.SP_TOKEN) +
                "\",\"bb\":\"" + BuildConfig.VERSION_CODE +
                "\",\"lat\":\"" + pos.get("lat") +
                "\",\"lon\":\"" + pos.get("lon") +
                "\",\"purl\":\"" + url +
                "\",\"zwid\":\"" + zwid +
                "\",\"timestamp\":" + t + "}";
        LogUtils.e(TAG, json);
        String encrypt = null;
        try {
            encrypt = AESHttpUtil.加密(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RxHttp.postJson(Constants.URL_SC)
                .add("encrypt", encrypt)
                .add("timestamp", t)
                .asResponse(String.class);
    }

    public Observable<String> token() {
        long t = new Date().getTime();
        String json = "{\"bb\":\"" + BuildConfig.VERSION_CODE + "\",\"timestamp\":" + t + "}";
        LogUtils.e(TAG, json);
        String encrypt = null;
        try {
            encrypt = AESHttpUtil.加密(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RxHttp.postJson(Constants.URL_GETTOKEN)
                .add("encrypt", encrypt)
                .add("timestamp", t)
                .asString();
    }

}
